package com.nbsaas.boot;

import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;

public class WebServer {
    private final HttpServer server;
    private final Map<String, HttpHandler> routes = new HashMap<>();

    public WebServer(int port) throws IOException {
        this.server = HttpServer.create(new InetSocketAddress(port), 0);
    }

    public void start() {
        for (Map.Entry<String, HttpHandler> entry : routes.entrySet()) {
            server.createContext(entry.getKey(), entry.getValue());
        }
        server.start();
        System.out.println("Server started on port " + server.getAddress().getPort());
    }

    public void route(String path, HttpHandler handler) {
        routes.put(path, handler);
    }

    public void stop() {
        server.stop(0);
    }
}
